#!/usr/bin/env python
# -*- coding: utf8 -*-
import models, datetime

def getOffset(type):
    if type == 'SpecialNewPages':
        return _getOffsetSpecialNewPages()
    query = models.offset.all()
    query.filter('type =', type)
    data = query.get()
    if data:
        return data.offset
    else:
        data = models.offset(type = type,
                             offset = 0)
        data.put()
        return 0

def putOffset(type, offset):
    query = models.offset.all()
    query.filter('type =', type)
    data = query.get()
    if data:
        data.offset = offset
        data.put()
        return True
    else:
        return False

def _getOffsetSpecialNewPages():
    query = models.NewPage.all()
    query.order('pubDate')
    data = query.get()
    if data:
        pubDate = data.pubDate
        offset = int(pubDate.strftime('%Y%m%d%H%M%S'))
    else:
        offset = None
    return offset